#define  _CRT_SECURE_NO_WARNINGS
class Solution {

public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<bool>> visit(m, vector<bool>(n, false));
        int ret = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!visit[i][j] && grid[i][j] == 1)
                {
                    ret = max(ret, src(grid, i, j, visit));
                }
            }
        }
        return ret;
    }
    int src(vector<vector<int>>& grid, int sr, int sc, vector<vector<bool>>& visit)
    {
        queue<pair<int, int>> q;
        int ret = 1;
        q.push({ sr, sc });
        visit[sr][sc] = true;
        while (q.size())
        {
            auto [r, c] = q.front(); q.pop();
            static int dx[4] = { 0,0,1,-1 };
            static int dy[4] = { 1,-1,0,0 };
            for (int i = 0; i < 4; i++)
            {
                int x = r + dx[i];
                int y = c + dy[i];
                if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()
                    && !visit[x][y] && grid[x][y] == 1)
                    q.push({ x, y }), ret++, visit[x][y] = true;
            }
        }
        return ret;
    }
};